121. 买卖股票的最佳时机
为保证权益,题目请参考 121. 买卖股票的最佳时机(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int buy = INT_MAX;
int sell = INT_MIN;
int ans = 0;
for (int i = 0; i < prices.size(); ++i)
{
if (prices[i] < buy)
{
buy = prices[i];
sell = prices[i];
}
else if (prices[i] > sell)
{
sell = prices[i];
}
if (sell - buy > ans)
{
ans = sell - buy;
}
}
return ans;
}
};
int main()
{
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38